/**
 * 
 */
package com.pks.autoInsurance.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.pks.autoInsurance.dto.VehicleDto;

/**
 * Implementation class for VehicleDAO.
 * 
 * @author Pankaj Soni
 * 
 */
public class VehicleDAOImpl extends AutoInsuranceDAO implements VehicleDAO {

	/**
	 * @see com.pks.autoInsurance.dao.VehicleDAO#getVehicle(java.lang.String)
	 */
	@Override
	public VehicleDto getVehicle(String ssn) {
		System.out.println(getClass() + "::::getUserVehicleDetail::::begin");
		Connection connection = getConnection();
		String sql = "SELECT TYPE,MODEL,MAKE,MF_YEAR,REGNO,POLICY_TYPE,POLICY_AMOUNT,TOTAL_ACCIDENT,QUOTE_DATE FROM VEHICLE WHERE SSN=?";
		try {
			connection.setAutoCommit(false);
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setString(1, ssn);
			ResultSet resultSet = preparedStatement.executeQuery();
			VehicleDto vehicleDetailsDto = new VehicleDto();
			if (resultSet.next()) {
				vehicleDetailsDto.setSsn(ssn);
				vehicleDetailsDto.setType(resultSet.getString("TYPE"));
				vehicleDetailsDto.setModel(resultSet.getString("MODEL"));
				vehicleDetailsDto.setMake(resultSet.getString("MAKE"));
				vehicleDetailsDto.setMfYear(resultSet.getString("MF_YEAR"));
				vehicleDetailsDto.setRegNo(resultSet.getString("REGNO"));
				vehicleDetailsDto.setPolicyType(resultSet
						.getString("POLICY_TYPE"));
				vehicleDetailsDto.setPolicyAmount(resultSet
						.getInt("POLICY_AMOUNT"));
				vehicleDetailsDto.setTotalAccident(resultSet
						.getInt("TOTAL_ACCIDENT"));
				vehicleDetailsDto.setQuoteDate(resultSet.getDate("QUOTE_DATE"));
			}
			System.out.println(getClass() + "::::getUserVehicleDetail::::end");
			return vehicleDetailsDto;
		} catch (SQLException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
			return null;
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				System.err.println(getClass() + "::::" + e.getMessage());
			}
		}
	}

	/**
	 * @see com.pks.autoInsurance.dao.VehicleDAO#updateVehicle(com.pks.autoInsurance.dto.VehicleDto)
	 */
	@Override
	public boolean updateVehicle(VehicleDto vehicleDtoReq) {
		System.out.println(getClass() + "::::updateVehicle::::begin");
		Connection connection = getConnection();
		String sqlVehicle = "UPDATE VEHICLE SET TYPE=?,MODEL=?,MAKE=?,"
				+ "REGNO=?,POLICY_TYPE=?,POLICY_AMOUNT=?,MF_YEAR=?,"
				+ "TOTAL_ACCIDENT=?,QUOTE_DATE=? WHERE SSN=?";
		try {
			connection.setAutoCommit(false);

			// insert data in vehicle table
			PreparedStatement preparedStatement = connection
					.prepareStatement(sqlVehicle);
			preparedStatement = connection.prepareStatement(sqlVehicle);
			preparedStatement.setString(1, vehicleDtoReq.getType());
			preparedStatement.setString(2, vehicleDtoReq.getModel());
			preparedStatement.setString(3, vehicleDtoReq.getMake());
			preparedStatement.setString(4, vehicleDtoReq.getRegNo());
			preparedStatement.setString(5, vehicleDtoReq.getPolicyType());
			preparedStatement.setInt(6, vehicleDtoReq.getPolicyAmount());
			preparedStatement.setString(7, vehicleDtoReq.getMfYear());
			preparedStatement.setInt(8, vehicleDtoReq.getTotalAccident());
			preparedStatement.setDate(9, new java.sql.Date(vehicleDtoReq
					.getQuoteDate().getTime()));
			preparedStatement.setString(10, vehicleDtoReq.getSsn());
			int insertedVechileRows = preparedStatement.executeUpdate();
			System.out.println(getClass() + ":::insertedVechileRows:::"
					+ insertedVechileRows);
		} catch (SQLException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException ex) {
				System.err.println(getClass() + "::::" + ex.getMessage());
				return false;
			}
		} finally {
			try {
				connection.commit();
				connection.close();
			} catch (SQLException e) {
				System.err.println(getClass() + "::::" + e.getMessage());
			}
		}
		System.out.println(getClass() + "::::updateVehicle::::end");
		return true;

	}

}
